package com.example.gymmanagement.controller;

import com.example.gymmanagement.Utils.Result;
import com.example.gymmanagement.Utils.StatusCode;
import com.example.gymmanagement.domain.Field;
import com.example.gymmanagement.domain.Matchs;
import com.example.gymmanagement.domain.MatchsSave;
import com.example.gymmanagement.domain.User;
import com.example.gymmanagement.service.MatchsService;
import com.example.gymmanagement.service.UserService;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 *赛事管理
 */
@RestController
@RequestMapping("/match")
public class MatchsController {
    @Autowired
    private MatchsService matchsService;
    @Autowired
    private UserService userService;

    @PostMapping("/save")
    @ApiOperation("赛事保存")
    //public Result save(@RequestBody Matchs matchs,@RequestBody int timeNum ,@RequestBody int light,@RequestHeader("Authorization") String token){
    public Result save(@RequestBody MatchsSave matchsSave, @RequestHeader("Authorization") String token){
        Matchs matchs = matchsSave.getMatchs();
        int timeNum = matchsSave.getTimeNum();
        int light = matchsSave.getLight();
        Result result = this.userService.selectCurrentUser(token);
        User user = (User) result.getData();
        Long userNum = user.getUserNum();
        matchs.setMatchCreatetime(LocalDateTime.now());
        matchs.setMatchStart(LocalDateTime.now());
        matchs.setMatchEnd(LocalDateTime.now().plusHours(timeNum));
        matchs.setMatchOwner(userNum);
        return matchsService.insert(matchs,light);
    }

    @ApiOperation("查询赛事（分页）")
    @GetMapping("/listPage")
    public Result listPage(int index, int size, Matchs matchs,@RequestHeader("Authorization") String token){
        Result fieldList = this.matchsService.selectList(index,size,matchs);
        return fieldList;
    }

    @ApiOperation("按id修改赛事")
    @PostMapping("/updateMatch")
    public Result updateMatch(@RequestBody Matchs matchs,@RequestHeader("Authorization") String token){
        matchs.setMatchUpdatetime(LocalDateTime.now());
        boolean flag = matchsService.updateMatch(matchs);
        String msg = flag?"修改场地成功":"修改场地失败";
        return new Result(flag? StatusCode.UPDATE_OK:StatusCode.UPDATE_ERR,matchs,msg);
    }

    @ApiOperation("按id删除赛事")
    @GetMapping("/deleteById")
    public Result deleteById(int id,@RequestHeader("Authorization") String token){
        boolean flag = matchsService.deleteById(id);
        String msg = flag?"删除赛事成功":"删除赛事失败";
        return new Result(flag? StatusCode.SAVE_OK:StatusCode.SAVE_ERR,"操作赛事id为"+id,msg);
    }
}
